//
//  MovieListSingleton.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/11/3.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "MovieListSingleton.h"
#import <sqlite3.h>
@implementation MovieListSingleton

+ (instancetype)shareMovieList
{
    static MovieListSingleton *movieListBase = nil;
    if (movieListBase == nil) {
        movieListBase = [[MovieListSingleton alloc] init];
    }
    return movieListBase;
}

// 声明数据库对象
static sqlite3 *mb = nil;

//打开数据库
- (void)openMB
{
    if (mb != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    // 打开数据库
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"movie.sqlite"];
    NSLog(@"sqlPath = %@", sqlPath);
    BOOL result = sqlite3_open(sqlPath.UTF8String, &mb);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
}


// 创建表
- (void)createTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS dianying001(number INTEGER PRIMARY KEY AUTOINCREMENT, movieId TEXT,movieName TEXT, pic_url TEXT)";
    BOOL result = sqlite3_exec(mb, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }
}

//插入一个电影
- (void)insertMovie:(Movie *)movie
{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO dianying001(movieId, movieName, pic_url) VALUES('%@','%@', '%@')", movie.movieId,movie.movieName, movie.pic_url];
    BOOL result = sqlite3_exec(mb, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

// 查询
- (NSMutableArray *)selectMovie
{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = @"SELECT * FROM dianying001";
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare(mb, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        NSLog(@"查询准备失败");
    }
    
    // 循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        // 取出第一列数据
        const unsigned char *movieId = sqlite3_column_text(stmt, 1);
        const unsigned char *movieName = sqlite3_column_text(stmt, 2);
        const unsigned char *pic_url = sqlite3_column_text(stmt, 3);
        // 将C转化OC
        NSString *idSter = [NSString stringWithUTF8String:(const char *) movieId];
        NSString *nameSter = [NSString stringWithUTF8String:(const char *) movieName];
        NSString *picSter = [NSString stringWithUTF8String:(const char *) pic_url];
        
        // 创建对象
        Movie *mov = [[Movie alloc] init];
        mov.movieId = idSter;
        mov.movieName = nameSter;
        mov.pic_url = picSter;
        [array addObject:mov];
    
    }
    NSLog(@"array ===  %@",array);
    return array;
}

// 删除
- (void)dropTable
{
    NSString *sql = @"DROP TABLE dianying001";
    BOOL result = sqlite3_exec(mb, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
}





@end
